//
//  HQYFoodKindViewController.m
//  Go!
//
//  Created by rimi on 16/7/13.
//  Copyright © 2016年 黄秋燕. All rights reserved.
//

#import "HQYFoodKindViewController.h"
#import "HQYFoodTableModel.h" // 列表模型
#import "HQYFoodTableCell.h" // 列表cell
#import "UIImageView+WebCache.h" // 图片缓存
#import "HQYSearchViewController.h" // 搜索页面控制器
#import "HQYHomeCellDetailViewController.h" 

#define tableviewCellId @"kTableviewCellIdKey"

@interface HQYFoodKindViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView; /**< 美食表格视图 */
@property (nonatomic, strong) NSArray *tableModelArray; /**< 列表数据数组 */
@property (nonatomic, strong) HQYFoodTableModel *model; /**< 数据模型 */
@end

@implementation HQYFoodKindViewController

- (void)dealloc {
    // 移除观察者
    [self.model removeObserver:self forKeyPath:@"responseDetail"];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.model getListWithClassID:_classId FoodId:_foodId];
    [self.model addObserver:self forKeyPath:@"responseDetail" options:NSKeyValueObservingOptionNew context:nil];
    
    /**< 搜索按钮 */
    UIBarButtonItem *searchItem = [[UIBarButtonItem alloc]initWithImage:[IMAGE(@"搜索-红-55") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(clickSearchItem)];
    self.navigationItem.rightBarButtonItem = searchItem;
    
    
    //注册原型cell（xib模式）
    [self.tableView registerNib:[UINib nibWithNibName:@"HQYFoodTableCell" bundle:nil] forCellReuseIdentifier:tableviewCellId];
    
    [self.view addSubview:self.tableView];
}

// 监听到状态变化会执行该方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"responseDetail"]) {
        self.tableModelArray = [self.model getDetailDataArray];
        [self.tableView reloadData];
    }
}

#pragma mark - 搜索按钮监听
- (void)clickSearchItem {
    HQYSearchViewController *searchVC = [[HQYSearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}

#pragma mark - <UITableViewDelegate, UITableViewDataSource>
// 返回有多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.tableModelArray.count;
}

// 每组多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return ((NSArray *)self.tableModelArray[section][@"shops"]).count;
}

// 每行是什么
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HQYFoodTableCell *cell = [tableView dequeueReusableCellWithIdentifier:tableviewCellId];
    
    NSDictionary *dict = self.tableModelArray[indexPath.section][@"shops"][indexPath.row];
    
    [cell.foodIconImage sd_setImageWithURL:[NSURL URLWithString:dict[@"icon"]] placeholderImage:IMAGE(@"等待中")];
    cell.foodNameLabel.text = dict[@"name"];
    cell.foodPriceLabel.text = dict[@"price"];
    cell.foodSellNumLabel.text = dict[@"sellNum"];
    
    return cell;
}

// 组头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return Size_H(50);
}

// 组底部视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

// 返回组模式下每个组头视图的代理方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, Size_H(50))];
    headView.backgroundColor = [UIColor whiteColor];
    
    UILabel_Font *shopNameLabel = [[UILabel_Font alloc] initWithFrame:CGRectMake(Size_W(12), 0, headView.frame.size.width, headView.frame.size.height)];
    shopNameLabel.font = [UIFont systemFontOfSize:Font(20)];
    NSDictionary *dict = self.tableModelArray[section];
    shopNameLabel.text = dict[@"shopName"];
    [headView addSubview:shopNameLabel];
    
    return headView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HQYHomeCellDetailViewController *detailVC = [[HQYHomeCellDetailViewController alloc] init];
//    detailVC.title = _tableModelArray[indexPath.section][@"shops"][indexPath.row][@"name"];
    detailVC.dict = _tableModelArray[indexPath.section][@"shops"][indexPath.row];
    detailVC.url = _tableModelArray[indexPath.section][@"shops"][indexPath.row][@"url"];
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - Getter
/**< 美食表格视图 */
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH) style:UITableViewStyleGrouped];
        _tableView.rowHeight = Size_H(80);
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

- (HQYFoodTableModel *)model {
    if (!_model) {
        _model = [[HQYFoodTableModel alloc] init];
    }
    return _model;
}
@end
